package com.smartwasser.yunhtgl.apphtgl.activity;

import android.app.Dialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.smartwasser.yunhtgl.apphtgl.App;
import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.bean.LoginBean;
import com.smartwasser.yunhtgl.apphtgl.bean.UpdateVersionBean;
import com.smartwasser.yunhtgl.apphtgl.network.Retrofits;
import com.smartwasser.yunhtgl.apphtgl.presenter.LoginPresenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.LoginPresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.presenter.UpdateVersionPresenter;
import com.smartwasser.yunhtgl.apphtgl.presenter.UpdateVersionPresenterImpl;
import com.smartwasser.yunhtgl.apphtgl.utils.DialogUI;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;
import com.smartwasser.yunhtgl.apphtgl.view.LoginView;
import com.smartwasser.yunhtgl.apphtgl.view.UpdateVersionView;
import com.smartwasser.yunhtgl.apphtgl.widget.CustomToast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.dou361.dialogui.DialogUIUtils.dismiss;
import static com.dou361.dialogui.DialogUIUtils.showAlertHorizontal;
import static com.smartwasser.yunhtgl.apphtgl.utils.BaseUtils.md5;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.getStringData;
import static com.smartwasser.yunhtgl.apphtgl.utils.SharedUtils.setStringData;
import static com.smartwasser.yunhtgl.apphtgl.widget.CustomToast.showToast;

/**
 * 系统选择
 */
public class SysActivity extends BaseActivity implements LoginView,UpdateVersionView {
    private LoginPresenter loginPresenter = new LoginPresenterImpl(this);
    private UpdateVersionPresenter updateVersionPresenter = new UpdateVersionPresenterImpl(this);
    private Dialog loadingHorizontal;
    private String user, pass;

    private String now_version="";

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_sys);
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        user = getStringData(this, "login_name", "");
        pass = getStringData(this, "login_password", "");
        getVersion();
        checkVersion();
    }

    /**
     * 获取当前版本号
     */
    private void getVersion() {
        try {
            PackageInfo packageInfo = getApplicationContext().getPackageManager().getPackageInfo(getPackageName(),0);
            now_version = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * 检查版本
     */
    private void checkVersion() {
        Retrofits.clean();
        App.getInstance().setSystem("2");
        updateVersionPresenter.version();
    }

    @OnClick({R.id.c_jt, R.id.c_shj})
    @Override
    public void onClick(View view) {
        Logger.e((App.getInstance().getSystem()));
        switch (view.getId()) {
            case R.id.c_jt:
                Retrofits.clean();
                if ("0".equals(App.getInstance().getSystem())) {
                    startActivity(MainActivity.class);
                    finish();
                } else {
                    App.getInstance().setSystem("0");
                    loadingHorizontal = showLoadingHorizontal(this, "登陆中...").show();
                    loginPresenter.login(user, md5(pass));
                }
                break;
            case R.id.c_shj:
                Retrofits.clean();
                if ("1".equals(App.getInstance().getSystem())) {
                    startActivity(MainActivity.class);
                    finish();
                } else {
                    App.getInstance().setSystem("1");
                    loadingHorizontal = showLoadingHorizontal(this, "登陆中...").show();
                    loginPresenter.login(user, md5(pass));
                }
                break;
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                CustomToast.showToast(this, "再按一次退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onSuccess(LoginBean loginBean) {
        dismiss(loadingHorizontal);
        String jessionid = loginBean.getJSESSIONID();
        if (jessionid == null) {
            App.getInstance().setSystem("2");
            showToast(SysActivity.this, loginBean.getMessage());
        } else {
            setStringData(this, "JSESSIONID", loginBean.getJSESSIONID());
            startActivity(MainActivity.class);
            finish();
        }
    }

    @Override
    public void onSuccessVersion(final UpdateVersionBean updateVersionBean) {
        if(!updateVersionBean.getVersionShort().equals(now_version)){
            showAlertHorizontal(this, "更新提示", updateVersionBean.getChangelog(), new DialogUI() {
                @Override
                public void onPositive() {
                    try {
                        showToast(SysActivity.this,updateVersionBean.getChangelog());
                    } catch (Exception e) {
                        Logger.e("请求网络大法启动失败");
                    }
                }
            }).show();

        }

    }

    @Override
    public void OnErrorVersion() {

    }

    @Override
    public void OnError() {
        App.getInstance().setSystem("");
        dismiss(loadingHorizontal);
        showError();
    }
}